<?php

/*
 +--------------------------------------------------------------------+
 | CiviCRM version 3.3                                                |
 +--------------------------------------------------------------------+
 | Copyright CiviCRM LLC (c) 2004-2010                                |
 +--------------------------------------------------------------------+
 | This file is a part of CiviCRM.                                    |
 |                                                                    |
 | CiviCRM is free software; you can copy, modify, and distribute it  |
 | under the terms of the GNU Affero General Public License           |
 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
 |                                                                    |
 | CiviCRM is distributed in the hope that it will be useful, but     |
 | WITHOUT ANY WARRANTY; without even the implied warranty of         |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
 | See the GNU Affero General Public License for more details.        |
 |                                                                    |
 | You should have received a copy of the GNU Affero General Public   |
 | License and the CiviCRM Licensing Exception along                  |
 | with this program; if not, contact CiviCRM LLC                     |
 | at info[AT]civicrm[DOT]org. If you have questions about the        |
 | GNU Affero General Public License or the licensing of CiviCRM,     |
 | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
 +--------------------------------------------------------------------+
*/

/**
 *
 * @package CRM
 * @copyright CiviCRM LLC (c) 2004-2010
 * $Id$
 *
 */

require_once 'CRM/Report/Form.php';

class CRM_Report_Form_Contact_Log extends CRM_Report_Form {

    protected $_summary      = null;
    
    function __construct( ) {		

    	$this->activityTypes = CRM_Core_PseudoConstant::activityType(true, true);
        asort($this->activityTypes);
    	
        $this->_columns = 
            array( 'civicrm_contact' =>
                   array( 'dao'       => 'CRM_Contact_DAO_Contact',
                          'fields'    =>
                          array( 'display_name' => 
                                 array( 'title'     => ts( 'Modified By' ),
                                        'required'  => true,),
                                 'id'           => 
                                 array( 'no_display'=> true,
                                        'required'  => true, ), ),
                          'filters'   =>             
                          array( 'sort_name'    => 
                                 array( 'title'      => ts( 'Modified By' )  ),
                          ),
                          'grouping'  => 'contact-fields',
                        ),

                   'civicrm_contact_touched' =>
                   array( 'dao'       => 'CRM_Contact_DAO_Contact',
                          'fields'    =>
                          array( 'display_name_touched' => 
                                 array( 'title'     => ts( 'Touched Contact' ),
                                        'name'      => 'display_name',
                                        'required'  => true,),
                                 'id'       => 
                                 array( 'no_display'=> true,
                                        'required'  => true, ), ),
                          'filters'   =>             
                          array( 'sort_name_touched'    => 
                                 array( 'title'      => ts( 'Touched Contact' ),
                                        'name'       => 'sort_name',
                                      ),
                                ),
                          'grouping'  => 'contact-fields',
                          ),
                          
                  'civicrm_activity' => 
                   array( 'dao'       => 'CRM_Activity_DAO_Activity',
                          'fields'    =>
                          array( 'id'  => array('title' => ts('Activity ID'),
                                                'no_display' => true,
                                                'required' => true,
                                               ),
                                 'subject'  => array('title' => ts('Touched Activity'),
                                                'required' => true,
                                               ),
                                 'activity_type_id'  => array('title' => ts( 'Activity Type' ),
                                                'required' => true,
                                               ),
                                 'source_contact_id'  => array('no_display' => true,
                                                'required' => true,
                                               ),
                              ),
                          ),

                   'civicrm_log' => 
                   array( 'dao'    => 'CRM_Core_DAO_Log',
                          'fields'    =>
                          array( 'modified_date' => 
                                 array( 'title'     => ts( 'Modified Date' ),
                                        'required'  => true,
                                       ),
                                 'data' => 
                                 array( 'title'     => ts( 'Description' ),
                                       ),
                                ),
                          'filters' =>             
                          array( 'modified_date' => 
                                 array( 'title'     => ts( 'Modified Date' ),
                                        'operatorType' => CRM_Report_Form::OP_DATE,
                                        'type'    => CRM_Utils_Type::T_DATE,
                                        'default' => 'this.week',
                                       ),
                                ), 
                          ),
                   );

        parent::__construct( );
    }
    
    function preProcess( ) {
        parent::preProcess( );
    }
    
    function select( ) {
        $select = array( );
        $this->_columnHeaders = array( );
        foreach ( $this->_columns as $tableName => $table ) {
            if ( array_key_exists('fields', $table) ) {
                foreach ( $table['fields'] as $fieldName => $field ) {
                    if ( CRM_Utils_Array::value( 'required', $field ) ||
                         CRM_Utils_Array::value( $fieldName, $this->_params['fields'] ) ) {

                        $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
                        $this->_columnHeaders["{$tableName}_{$fieldName}"]['type']  = CRM_Utils_Array::value( 'type', $field );
                        $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
                    }
                }
            }
        }

        $this->_select = "SELECT " . implode( ', ', $select ) . " ";
    }

    static function formRule( $fields, $files, $self ) {  
        $errors = $grouping = array( );
        return $errors;
    }

    function from( ) {
        $this->_from = "
        FROM civicrm_log {$this->_aliases['civicrm_log']}
        inner join civicrm_contact {$this->_aliases['civicrm_contact']} on {$this->_aliases['civicrm_log']}.modified_id = {$this->_aliases['civicrm_contact']}.id
        left join civicrm_contact {$this->_aliases['civicrm_contact_touched']} on ({$this->_aliases['civicrm_log']}.entity_table='civicrm_contact' AND {$this->_aliases['civicrm_log']}.entity_id = {$this->_aliases['civicrm_contact_touched']}.id)
        left join civicrm_activity {$this->_aliases['civicrm_activity']} on ({$this->_aliases['civicrm_log']}.entity_table='civicrm_activity' AND {$this->_aliases['civicrm_log']}.entity_id = {$this->_aliases['civicrm_activity']}.id)
        ";
            
    }

    function where( ) {
        $clauses = array( );
        $this->_having = '';
        foreach ( $this->_columns as $tableName => $table ) {
            if ( array_key_exists('filters', $table) ) {
                foreach ( $table['filters'] as $fieldName => $field ) {
                    $clause = null;
                    if ( $field['operatorType'] & CRM_Report_Form::OP_DATE ) {
                        $relative = CRM_Utils_Array::value( "{$fieldName}_relative", $this->_params );
                        $from     = CRM_Utils_Array::value( "{$fieldName}_from"    , $this->_params );
                        $to       = CRM_Utils_Array::value( "{$fieldName}_to"      , $this->_params );
                        
                        $clause = $this->dateClause( $field['dbAlias'], $relative, $from, $to );
                    } else {
                        $op = CRM_Utils_Array::value( "{$fieldName}_op", $this->_params );
                        if ( $op ) {
                            $clause = 
                                $this->whereClause( $field,
                                                $op,
                                                CRM_Utils_Array::value( "{$fieldName}_value", $this->_params ),
                                                CRM_Utils_Array::value( "{$fieldName}_min", $this->_params ),
                                                CRM_Utils_Array::value( "{$fieldName}_max", $this->_params ) );
                        }
                    }

                    if ( ! empty( $clause ) ) {
                        $clauses[ ] = $clause;
                    }
                }
            }
        }

        $clauses[] = "({$this->_aliases['civicrm_log']}.entity_table <> 'civicrm_domain')";
        $this->_where = "WHERE " . implode( ' AND ', $clauses );
        
    }
    
    function orderBy( ) {
        $this->_orderBy = "
ORDER BY {$this->_aliases['civicrm_log']}.modified_date DESC
";
    }
    
/*    function postProcess( ) {

        $this->beginPostProcess( );

        $sql  = $this->buildQuery( true );
//CRM_Core_Error::debug('sql', $sql);             
        $rows = $graphRows = array();
        $this->buildRows ( $sql, $rows );
        
        $this->formatDisplay( $rows );
        $this->doTemplateAssignment( $rows );
        $this->endPostProcess( $rows );	
    }
*/
    
    function alterDisplay( &$rows ) {
        // custom code to alter rows
        $entryFound = false;
        foreach ( $rows as $rowNum => $row ) {
            // convert display name to links
            if ( array_key_exists('civicrm_contact_display_name', $row) && 
                 array_key_exists('civicrm_contact_id', $row) ) {
                $url = CRM_Utils_System::url( 'civicrm/contact/view', 
                                              'reset=1&cid=' . $row['civicrm_contact_id'],
                                              $this->_absoluteUrl );
                $rows[$rowNum]['civicrm_contact_display_name_link' ] = $url;
                $rows[$rowNum]['civicrm_contact_display_name_hover'] = ts("View Contact details for this contact.");
                $entryFound = true;
            }

            if ( array_key_exists('civicrm_contact_touched_display_name_touched', $row) && 
                 array_key_exists('civicrm_contact_touched_id', $row) &&
                 $row['civicrm_contact_touched_display_name_touched'] !== '' ) {
                $url = CRM_Utils_System::url( 'civicrm/contact/view', 
                                              'reset=1&cid=' . $row['civicrm_contact_touched_id'],
                                              $this->_absoluteUrl );
                $rows[$rowNum]['civicrm_contact_touched_display_name_touched_link' ] = $url;
                $rows[$rowNum]['civicrm_contact_touched_display_name_touched_hover'] = ts("View Contact details for this contact.");
                $entryFound = true;
            }

            if ( array_key_exists('civicrm_activity_subject', $row) && 
                 array_key_exists('civicrm_activity_id', $row) &&
                 $row['civicrm_activity_subject'] !== '' ) {
                $url = CRM_Utils_System::url( 'civicrm/contact/view/activity', 
                                              'reset=1&action=view&id=' . $row['civicrm_activity_id'] . '&cid=' . $row['civicrm_activity_source_contact_id'] . '&atype=' . $row['civicrm_activity_activity_type_id'],
                                              $this->_absoluteUrl );
                $rows[$rowNum]['civicrm_activity_subject_link' ] = $url;
                $rows[$rowNum]['civicrm_activity_subject_hover'] = ts("View Contact details for this contact.");
                $entryFound = true;
            }

            if ( array_key_exists('civicrm_activity_activity_type_id', $row ) ) {
                if ( $value = $row['civicrm_activity_activity_type_id'] ) {
                    $rows[$rowNum]['civicrm_activity_activity_type_id'] = $this->activityTypes[$value];
                }
                $entryFound = true;
            }
            
            // skip looking further in rows, if first row itself doesn't 
            // have the column we need
            if ( !$entryFound ) {
                break;
            }
        }
    }
    
}
